package cate.game.admin.menu.demo;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.common.debug.DevDebugCmd;
import cate.game.db.GameMongoDao;
import cate.game.demo.msg.DemoEchoBinResp;
import cate.game.demo.msg.DemoEchoTextResp;
import cate.game.demo.po.DemoApple;
import cate.game.demo.po.DemoPerson;
import cate.game.fight.test.TestFightCtx;
import cate.game.mail.po.MailBuilder;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.vo.PlayVO;
import cate.game.play.vo.ReplayUtils;
import cate.game.role.mail.Mail;
import cate.game.world.msg.GiftCodeUseReq;
import cp.solution.fax.FaxTags;
import cp.solution.manage.api.open.replay.ReplaySaveReq;
import cp.solution.manage.api.open.replay.ReplaySaveResp;
import cp.solution.rpc.path.RpcSharePath;
import easy.java.practice.errorcode.EcResult;
import fax.client.NodeRequester;
import fax.common.route.RouteMeta;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

@Slf4j
public class DemoMenu extends MenuSetup {

    private static int count;

    public DemoMenu(GameBody game) {
        super("demo");

        reg(game, "遍历角色合服结算", 0, "参数无", (role, params) -> {
            game.role.handleMergeSettle();
        });

        reg(game, "版本校对", 0, "无参数", (role, params) -> {
            String verTime = "0811/21:05";
            String desc = "新内容：";
            desc += "MongoOidPo ";
            desc += "【段位赛】排名请求";
            desc += "TIME_OFFSET_RESET ";
            Mail mail = new MailBuilder().setTitle("版本信息"+verTime).setContent(desc).build();
            role.getMail().add(mail);
            game.notice.message(role, desc);
        });

        reg(game, "status修改", 1, "参数1：内容", (role, params) -> {
            String txt = toString(params, 0, "xxx");
            game.status.data.note = txt;
            game.notice.message(role, "状态笔记改为："+game.status.data.note);
        });

        reg(game, "status读取", 0, "参数1：无", (role, params) -> {
            game.notice.message(role, "状态笔记是："+game.status.data.note);
        });

        reg(game, "Kv测试demo改名", 1, "参数1：名称", (role, params) -> {
            String name = toString(params, 0, "新名字"+ ++count);
            game.demo.status.changeName(name);
            game.notice.message(role, "改名："+name);
        });

        reg(game, "Kv测试demo存储", 0, "参数1：无", (role, params) -> {
            game.demo.saveStatus();
            game.notice.message(role, "存储成功");
        });

        reg(game, "消息性能测试", 2, "参数1: 0:Bin和Text消息都发，1只发bin，2只发Text; 参数2：内容数量(1w以内,默认100)", (role, params) -> {
            int cmd = toInt(params, 0, 0);
            int dataLen = toInt(params, 1, 100);
            boolean sendBin = cmd == 0 || cmd == 1;
            boolean sendText = cmd == 0 || cmd == 2;
            if(sendBin) {
                role.sendNow(new DemoEchoBinResp(dataLen));
            }
            if(sendText) {
                role.sendNow(new DemoEchoTextResp(dataLen));
            }
        });


        reg(game, "礼包码", 1, "参数1：礼包码", (role, params) -> {
            String code = toString(params, 0, "02YQR23yK");
            GiftCodeUseReq req = new GiftCodeUseReq();
            req.code = code;
            req.h(game, role);
        });

        reg(game, "openApi之录像保存", 0, "", (role, params) -> {
            EcResult<FightSide> a = game.fight.side.createFightSideByRole(GDFight.Team.A, role, GDFunc.ADVENTURE);
            EcResult<FightSide> b = game.fight.side.createFightSideByRole(GDFight.Team.B, role, GDFunc.ADVENTURE);
            EcResult<PlayPO> r = game.play.startBySystem(new TestFightCtx(), a.data, b.data);
            PlayVO playVO = new PlayVO().read(r.data);
            long afterOneMinutes = System.currentTimeMillis() + 60000;

            try {
                ReplaySaveReq req = ReplayUtils.toReplaySaveReq(playVO, afterOneMinutes);

                // 异步使用模式
                game.openApi().saveReplay(req, (er) -> {
                    log.info("openApi之保存录像(异步)的结果是{}", er);
                });

                // 同步使用模式
                EcResult<ReplaySaveResp> resp = game.openApi().saveReplay(req);
                log.info("openApi之保存录像(同步)的结果是{}", resp);
            } catch (Exception e) {
                log.error("", e);
            }
        });

        reg(game, "role::get覆盖率", 0, "参数:无", (role, params) -> {
            log.info("覆盖信息：{}", role.coverInfo());
        });

        reg(game, "logcat", 2, "参数1:tag, 参数2:message", (role, params) -> {
            String tag = toString(params, 0, "演示");
            String message = toString(params, 1, "测试内容哦123:321?!\r\n前面是RN\r前面是R\n前面是N");
            game.logcat().log(tag, message);
        });

        reg(game, "部件序列化测试", 0, "参数:无", (role, params) -> {
            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(role.getBag());
                oos.flush();
                byte[] bytes = bos.toByteArray ();
                log.info("部件字节长度", bytes.length);
                oos.close();
                bos.close();
            } catch (Exception e) {
                log.error("", e);
            }
        });
        //

        reg(game, "统计暂时禁用", 0, "参数:无", (role, params) -> {
            log.info("已禁用统计");
            game.stat.disabled = true;
        });

        reg(game, "重置已经收到过的vip客服邮件对应的vip等级", 0, "参数:无", (role, params) -> {
            log.info("重置已经收到过的vip客服邮件对应的vip等级");
            role.getBase().uni.vipCsMailGotLv = 0;
        });


        reg(game, "dao hash", 0, "参数:无", (role, params) -> {
            StringBuilder sb = new StringBuilder();
            GameMongoDao gmd = game.facilities.getMongo();
            sb.append(gmd.roleBase.hashCode() + ", ");
            sb.append(gmd.roleBag.hashCode() + ", ");
            sb.append(gmd.roleFight.hashCode() + ", ");
            sb.append(gmd.roleMail.hashCode() + ", ");
            sb.append(gmd.roleFriend.hashCode() + ", ");
            sb.append(gmd.roleMall.hashCode() + ", ");
            sb.append(gmd.roleChat.hashCode() + ", ");
            sb.append(gmd.rolePay.hashCode() + ", ");
            sb.append(gmd.roleWorld.hashCode() + ", ");
            sb.append(gmd.roleNote.hashCode() + ", ");
            sb.append(gmd.roleSystem.hashCode() + ", ");
            sb.append(gmd.kv.hashCode() + ", ");
            sb.append(gmd.rank.hashCode() + ", ");
            sb.append(gmd.mailBox.hashCode() + ", ");
            sb.append(gmd.friendBox.hashCode() + ", ");
            sb.append(gmd.play.hashCode() + ", ");
            sb.append(gmd.status.hashCode() + ", ");
            sb.append(gmd.res.hashCode() + ", ");
            sb.append(gmd.farm.hashCode() + ", ");
            sb.append(gmd.guild.hashCode() + ", ");
            sb.append(gmd.pvp.hashCode() + ", ");
            sb.append(gmd.activity.hashCode() + ", ");
            sb.append(gmd.task.hashCode() + ", ");
            sb.append(gmd.history.hashCode() + ", ");
            log.info("hashcode={}", sb);
        });

    }
}
